package com.study.d2_reflect;

import org.junit.Test;

import java.lang.reflect.Field;

//目标：获取类的成员变量，并对其进行操作
public class Test3Field {
    @Test
    public void testGetFields() throws Exception {
        Class  c = Student.class;
//        获取类的全部成员变量
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getType()+"-->"+declaredField.getName());
        }
        //定位某个成员变量
        Field gender = c.getDeclaredField("gender");

        gender.setAccessible(true);
        Student s = new Student();
        gender.set(s,'女');
        char ch = (char)gender.get(s);
        System.out.println(ch);
    }
}
